package com.playcrab.adouban;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.Person;
import com.google.gdata.data.TextContent;
import com.google.gdata.data.douban.Author;
import com.google.gdata.data.douban.Entity;
import com.google.gdata.data.douban.ReviewEntry;
import com.google.gdata.data.douban.ReviewFeed;
import com.google.gdata.data.douban.DoumailEntry;
import com.google.gdata.data.douban.SubjectEntry;
import com.google.gdata.util.ServiceException;
import com.playcrab.adouban.util.DateTimeUtil;
import com.playcrab.adouban.util.DoubanAPI;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ViewDoumail extends BaseActivity {
	protected DoumailEntry doumail; 
	protected Handler handler = new Handler();
	protected String id = null;
	protected String type = null;
	private static String LOG_TAG = "ViewDoumail";

	private static final int MENU_DELETE_DOUMAIL = Menu.FIRST + 10000;
	private static final int MENU_EXIT = Menu.FIRST + 1000;
	private static final int MENU_REPLY = Menu.FIRST + 1001;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.view_doumail);
		Bundle bundle = this.getIntent().getExtras();
		type = bundle.getString("type");
		id = bundle.getString("id");
		Log.v(LOG_TAG, "type:" + type);
		Log.v(LOG_TAG, "id:" + id);

		this.showPending();

		new Thread() {
			public void run() {
				try {
					ViewDoumail.this.getDoumail();
					handler.post(new Runnable() {
						public void run() {
							ViewDoumail.this.showDoumail();
							ViewDoumail.this.hidePending();
						}
					});

				} catch (IOException e) {
					e.printStackTrace();
					ViewDoumail.this
							.showInfo(getString(R.string.network_error));
				} catch (ServiceException e) {
					e.printStackTrace();
					ViewDoumail.this
							.showInfo(getString(R.string.service_error));
				} finally {
					
				}
			}

		}.start();

	}

	public void showDoumail() {
		if (doumail == null) {
			return;
		}

		if (doumail.getTitle() != null) {
			TextView title = (TextView) findViewById(R.id.doumail_title);
			title.setText(doumail.getTitle().getPlainText());
		}
		if (doumail.getAuthors().size() > 0) {
			final Person p = doumail.getAuthors().get(0);
			TextView sender = (TextView) findViewById(R.id.doumail_sender);
			sender.setText(p.getName());
			sender.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					String sid = p.getUri().substring(
							p.getUri().lastIndexOf('/') + 1);
					ViewDoumail.this.viewUser(sid);
				}

			});

		}
		final Author receiver = doumail.getReceiver();
		if (receiver != null) {
			TextView receiver_view = (TextView) findViewById(R.id.doumail_receiver);
			receiver_view.setText(receiver.getName());

			receiver_view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					String sid = receiver.getUri().substring(
							receiver.getUri().lastIndexOf('/') + 1);
					ViewDoumail.this.viewUser(sid);
				}

			});
		}

		try {
			Author a = null;
			if (type.equals("outbox")) {
				a = doumail.getReceiver();
			} else {
				a = (Author) doumail.getAuthors().get(0);
			}

			final Author au = a;
			String url = a.getLink("icon", null).getHref();

			WebView image = (WebView) findViewById(R.id.view_doumail_image);
			image.loadUrl(url);
			image.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View arg0) {
					String sid = au.getUri().substring(
							au.getUri().lastIndexOf('/') + 1);
					ViewDoumail.this.viewUser(sid);
					return false;
				}

			});
		} catch (Exception e) {
			e.printStackTrace();
		}

		TextView content = (TextView) findViewById(R.id.doumail_content);
		content.setText(((TextContent) doumail.getContent()).getContent()
				.getPlainText());

		TextView published = (TextView) findViewById(R.id.doumail_published);
		published.setText(DateTimeUtil.simpleFormat(doumail.getPublished()));

	}

	public DoumailEntry getDoumail() throws IOException, ServiceException {
		DoubanService service = this.getAuthedDoubanService();
		doumail = service.getDoumail(Integer.parseInt(id));
		if (type.equals("inbox") && doumail.getUnread()) {
			// TODO: mark as read

		}

		return doumail;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		if (this.type.equals("inbox")) {
			menu.add(0, MENU_REPLY, 0, "回复").setIcon(
					android.R.drawable.ic_menu_set_as).setAlphabeticShortcut(
					'R');

		}

		menu.add(0, MENU_EXIT, 0, "返回").setIcon(android.R.drawable.ic_delete)
				.setAlphabeticShortcut('X');
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_DELETE_DOUMAIL: {
			return true;
		}
		case MENU_EXIT: {
			this.finish();
			return true;
		}
		case MENU_REPLY: {
			Person sender = doumail.getAuthors().get(0);
			Intent i = new Intent(ViewDoumail.this, ComposeDoumail.class);
			String title = doumail.getTitle().getPlainText();
			i.putExtra("to", sender.getUri());
			i.putExtra("toUserTitle", sender.getName());
			i.putExtra("title", getString(R.string.doumai_re).concat(title));
			startActivity(i);
			this.finish();
			return true;
		}

		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		menu.setHeaderTitle("操作菜单");
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {

		}

		return super.onOptionsItemSelected(item);

	}
}
